<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>匹配括号</title>
  </head>
  <body>
    <script>
      // 栈
      var isValid = function (s) {
        let map = {
          "(": -1,
          ")": 1,
          "[": -2,
          "]": 2,
          "{": -3,
          "}": 3,
        }
        let stack = []
        for (let i = 0; i < s.length; i++) {
          if (map[s[i]] < 0) {
            stack.push(s[i])
          } else {
            let last = stack.pop()
            if (map[last] + map[s[i]] != 0) return false
          }
        }
        if (stack.length > 0) return false
        return true
      }
    </script>
  </body>
</html>
